<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="moduleTitle" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
			<el-row>
				<el-col :span="12">
					<el-form-item label="原项目经理" prop="projectmanagercode" >
            <contractRefUser :disabled="true" v-model="model.projectmanagercode" :initDisplayData.sync="model.projectmanager" dataType="code" :singleSelect="true"></contractRefUser>
          </el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="项目经理" prop="newprojectmanagercode" :rules="rules.validateRequire">
            <contractRefUser :externalQueryParam="refUserParam" v-model="model.newprojectmanagercode" :initDisplayData.sync="model.newprojectmanager" dataType="code" :singleSelect="true" @confirm="confirmUser"></contractRefUser>
					</el-form-item>
				</el-col>
			</el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
import contractRefUser from "./contractRefUser"
/** 工具类 */
import { getSubStringFirstDeptCode } from '@/utils/mcrUtils'
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { contractRefUser },
  data() {
    return {
      moduleTitle: '项目经理变更',
      urlPrefix: '/sd/project/',
      url: {
        // 额外的url
        changeOwner: '/sd/project/changeOwner'
      },
      firstDeptCode: '',
      refUserParam: {},
      model: {}
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
    },
    open() {
      this.firstDeptCode = getSubStringFirstDeptCode(this.model.applydeptcode)
      this.refUserParam.qry_firstdeptcode = this.firstDeptCode
    },
    confirmUser(selectedData) {
      this.$set(this.model, 'newdeptcode', selectedData[0].deptcode)
      this.$set(this.model, 'newdeptname', selectedData[0].dispdeptid)
      this.$set(this.model, 'newprojectmanagercode', selectedData[0].code)
      this.$set(this.model, 'newprojectmanager', selectedData[0].name)
    },
    doSubmitEdit() {
      this.saveLoading = true
      if (!this.model.newprojectmanagercode) {
        this.$tap.alert("请选择新的项目经理", 'warning')
        this.saveLoading = false
        return
      }
      if (this.model.newprojectmanagercode === this.model.projectmanagercode) {
        this.$tap.alert("变更后的项目经理不允许和现有项目经理相同！")
        this.saveLoading = false
        return
      }
      this.$set(this.model, 'oldprojectmanagercode', this.model.projectmanagercode)
      this.$set(this.model, 'deptcode', this.model.newdeptcode)
      this.$set(this.model, 'deptname', this.model.newdeptname)
      this.$set(this.model, 'projectmanagercode', this.model.newprojectmanagercode)
      this.$set(this.model, 'projectmanager', this.model.newprojectmanager)
      // 增加唯一单号
      putAction(this.url.changeOwner, this.model)
        .then((res) => {
          if (res.success) {
            this.$tap.message('修改成功', 'success')
            this.visible = false
            this.$emit('refresh')
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch((err) => {
          this.saveLoading = false
        })
    },
    close() {
      this.model = {}
    }
  }
}
</script>

